﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

namespace Core.Helper
{
    public sealed class Image
    {
        /// <summary>
        /// 图片压缩
        /// </summary>
        /// <param name="original">原始图片路径</param>
        /// <param name="final">压缩后图片路径</param>
        /// <param name="width">压缩后宽度</param>
        /// <param name="height">压缩后高度</param>
        /// <param name="flag">压缩比，数值越小压缩比越高</param>
        /// <param name="size">压缩后大小</param>
        public static byte[] Compress(byte[] original, int? width, int? height, int flag = 90, string rgb = null)
        {
            System.Drawing.Image source = null;
            using (MemoryStream ms = new MemoryStream(original))
            {
                source = System.Drawing.Image.FromStream(ms);
            }

            if (source.IsNull())
            {
                throw new Exception("缺少图片源");
            }

            ImageFormat tFormat = source.RawFormat;
            int finalHeight = height ?? source.Height / 2;
            int finalWidth = width ?? source.Width / 2;
            int sourceWidth, sourceHeight;
            // 按比例缩放
            System.Drawing.Size sourceSize = new System.Drawing.Size(source.Width, source.Height);
            if (sourceSize.Width > finalHeight || sourceSize.Width > finalWidth)
            {
                if ((sourceSize.Width * finalHeight) > (sourceSize.Width * finalWidth))
                {
                    sourceWidth = finalWidth;
                    sourceHeight = (finalWidth * sourceSize.Height) / sourceSize.Width;
                }
                else
                {
                    sourceHeight = finalHeight;
                    sourceWidth = (sourceSize.Width * finalHeight) / sourceSize.Height;
                }
            }
            else
            {
                sourceWidth = sourceSize.Width;
                sourceHeight = sourceSize.Height;
            }

            Bitmap ob = new Bitmap(finalWidth, finalHeight);
            Graphics g = Graphics.FromImage(ob);

            if (rgb == null)
            {
                g.Clear(System.Drawing.Color.White);
            }
            else
            {
                string[] colors = rgb.Split(',');
                g.Clear(System.Drawing.Color.FromArgb(int.Parse(colors[0]), int.Parse(colors[1]), int.Parse(colors[2])));
            }
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            g.DrawImage(source, new Rectangle((finalWidth - sourceWidth) / 2, (finalHeight - sourceHeight) / 2, sourceWidth, sourceHeight), 0, 0, source.Width, source.Height, GraphicsUnit.Pixel);
            g.Dispose();

            // 以下代码为保存图片时，设置压缩质量
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            // 设置压缩的比例1-100
            qy[0] = flag;
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;

            try
            {
                ImageCodecInfo[] imageCodecInfos = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo imageCodecInfo = null;
                foreach (ImageCodecInfo item in imageCodecInfos)
                {
                    if (item.FormatDescription.Equals("PNG") || item.FormatDescription.Equals("JPEG"))
                    {
                        imageCodecInfo = item;
                        break;
                    }
                }

                using (MemoryStream ms = new MemoryStream())
                {
                    if (imageCodecInfo != null)
                    {
                        ob.Save(ms, imageCodecInfo, ep);
                    }
                    else
                    {
                        ob.Save(ms, tFormat);
                    }

                    return ms.ToArray();
                }
            }
            catch
            {
                return null;
            }
            finally
            {
                source.Dispose();
                ob.Dispose();
            }
        }

        public static void GenerateSeal(string content)
        {
            // 文字样式
            Font font = new Font("Arial", 12, FontStyle.Bold);
            int width = 80;
            int height = 40;
            // 设置圆画笔的粗细
            int thickness = 4;
            Rectangle rect = new Rectangle(0, 0, width - thickness, height - thickness);
            // 初始化画布
            Bitmap bMap = new Bitmap(80, 40);
            Graphics g = Graphics.FromImage(bMap);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.Clear(Color.White);
            // 画边
            Pen pen = new Pen(Color.Red, thickness);
            g.DrawRectangle(pen, rect);
            // 画字
            PointF xy = new PointF(width / 10, height / 4);
            g.DrawString(content, font, pen.Brush, xy);

            string name = content + ".gif";
            string path = Helper.Directory.MapPath($"/App_Resource/Files/Seal/{name}");
            bMap.Save(path);
        }
    }
}